package com.shinaier.laundry.snlstore.offlinecash.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shinaier.laundry.snlstore.R;
import com.shinaier.laundry.snlstore.network.entity.CooperativeFactoryEntity;
import com.shinaier.laundry.snlstore.offlinecash.adapter.ChooseCooperativeFactoryAdapter;

import java.util.List;


/**
 * 合作工厂popupwindow
 * Created by 张家洛 on 2017/5/11.
 */

public class ChooseCooperativeFactoryPop extends PopupWindow {
    private Context mContext;
    private View view;
    private ImageView close_view;
    private Handler handler;
    private List<CooperativeFactoryEntity.CooperativeFactoryResult> results;
    private String acceptId;

    public ChooseCooperativeFactoryPop(Context mContext, List<CooperativeFactoryEntity.CooperativeFactoryResult> results, Handler handler){
        this.mContext = mContext;
        this.results = results;
        this.handler = handler;
         /* 设置弹出窗口特征 */

        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
//        // 实例化一个ColorDrawable颜色为半透明
//        ColorDrawable dw = new ColorDrawable(0xb0000000);
//        // 设置弹出窗体的背景
//        this.setBackgroundDrawable(dw);
        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);
    }

    public void setView(){
        view = LayoutInflater.from(mContext).inflate(R.layout.cooperative_manufacturer_content, null);
        // 设置视图
        setContentView(view);
//        FlowLayout flRemarkContent = (FlowLayout) view.findViewById(R.id.fl_remark_content);
        close_view = (ImageView) view.findViewById(R.id.close_view);
        ListView singleTakeClothesList = (ListView) view.findViewById(R.id.single_take_clothes_list);
        TextView confirmIntoFactory = (TextView) view.findViewById(R.id.confirm_into_factory);


        if (results != null && results.size() > 0){
            final ChooseCooperativeFactoryAdapter chooseCooperativeFactoryAdapter = new ChooseCooperativeFactoryAdapter(mContext,
                    results);
            singleTakeClothesList.setAdapter(chooseCooperativeFactoryAdapter);
            chooseCooperativeFactoryAdapter.setSelectListener(new ChooseCooperativeFactoryAdapter.SelectFactoryListener() {
                @Override
                public void onSelectFactory(int position) {
                    for (int i = 0; i < results.size(); i++) {
                        results.get(i).isSelect = false;
                    }
                    results.get(position).isSelect = true;
                    acceptId = results.get(position).getAcceptId();
                    chooseCooperativeFactoryAdapter.notifyDataSetChanged();
                }
            });
            confirmIntoFactory.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (acceptId != null){
                        Message msg = new Message();
                        msg.obj = acceptId;
                        msg.what = 1;
                        handler.sendMessage(msg);
                        dismiss();
                    }

                }
            });
        }

        // 取消按钮
        close_view.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // 销毁弹出框
                dismiss();
            }
        });

        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = view.findViewById(R.id.ll_popupwindow).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });
    }
}
